<template>
     <div class="mycomment">
         <!-- <div class="mycomment-edit" @click="goEdit">{{edit?"取消":"编辑"}}</div> -->
         <!-- 这里添加的是下拉涮洗的组件 -->
       <mt-loadmore :top-method="loadTop" ref="loadmore" @top-status-change="handleTopChange">
          <div class="mycomment-content" v-for="(item,index) in dataList" :key="index" >
              <!-- <div v-if="edit" class="mypublish-content-left">
                      <input type="checkbox" :id="item.post_id" class="chk_1" ref="checkbox" @change="singleSelect" :cId="item.post_id" />
                      <label :for="item.post_id"></label>
              </div> -->
                <div class="mycomment-content-right" @click="goArtical(item.post_id)">
                     <p>{{item.create_time}}</p>
                     <p>{{item.content}}</p>
                     <div class="mycomment-content-right-content">
                           <div class="mycomment-content-right-content-top">
                               <p>{{item.title}}</p>
                           </div>
                           <div class="mycomment-content-right-content-bottom">
                               <div class="mycomment-content-right-icon clearfix">
                                    <a href="javascript:void(0)"><img src="../../../../assets/comment/pinglun@2x.png" alt=""><span class="red">{{item.num | number}}</span></a> 
                                    <a href="javascript:void(0)"><img src="../../../../assets/comment/guanzhu@2x.png" alt=""><span>{{item.favor_counts |  number}}</span></a>
                                    <a href="javascript:void(0)"><img src="../../../../assets/comment/dianzan@2x.png" alt=""><span>{{item.like_counts |  number}}</span></a>
                                    <a href="javascript:void(0)">
                                        <img src="../../../../assets/comment/liulanliang@2x.png" alt="">
                                        <span>{{item.reads_count |  number}}</span>              
                                    </a>   
                                </div>
                           </div>
                     </div>
                </div>
          </div>
           <!-- 注意这里是自定的下拉组件 -->
          <div slot="top" class="mint-loadmore-top">
                  <span v-show="topStatus === 'pull'" :class="{ 'rotate': topStatus === 'drop' }">↓   下拉刷新</span>
                  <div v-show="topStatus === 'loading'" class="lo">
                    <load-more :tip="('正在更新')"></load-more>
                  </div>
                  <span v-show="topStatus === 'drop'">↑   释放立即更新</span>
          </div>
       </mt-loadmore>
         
           <!-- 如果一条帖子也没有，那么空的图片则显示 -->
           <div class="showimg" v-if="this. dataList.length==0">
                 <img src="../../../../assets/nothing.png" alt="" >
                 <p>空空如也~</p>
                 <p><a href="javascript:void(0)" @click="skit">去逛逛论坛?</a></p>
           </div>
           <load-more :tip="('加载中...')" v-if="loadMoreShow"></load-more>
    <load-more :show-loading="false" :tip="('没有更多了')" background-color="#f5f5f5" v-if="noMoreShow"></load-more>
          <!-- 顶部的固定删除功能 -->
        <!-- <div class="deleteDo" v-if="edit">
            <div class="deleteDo-left">
                <label for="checkAll">
                <input type="checkbox" id="checkbox_a1" class="chk_1 checkall" ref="allSelect" @change="allSelect"/>
                <label for="checkbox_a1"></label>
                </label>
                <span style="position:absolute;">全选</span>
                <span>{{selectNum}}/{{ dataList.length}}</span>
            </div>
            <div class="deleteDo-right">删除</div>
        </div> -->
     </div>
</template>
<script>
import { dateFormat, LoadMore, cookie } from 'vux';
import { MessageBox, Toast } from 'mint-ui';
export default {
    data(){
        return {
          loadMoreShow: false,
          topStatus: '',
           pageNum: 1,
            edit: false,
            selectNum: 0,
            dataList: [],
            selectNum: 0,
            noMoreShow:false,
            arr: [],
            idStr: '',
            flag: true
        }
    },
    created() {
        var _this = this;
        this.$nextTick(function() {
            window.addEventListener('scroll', onScroll)
        })
        // 判断滚动条位置，加载更多
        function onScroll() {
            var scrollTop = document.body.scrollTop;
            var innerHeight = window.innerHeight;
            var offsetHeight = document.body.offsetHeight;
            if (scrollTop + innerHeight >= offsetHeight) {
                if (_this.flag) {
                    if (_this. dataList.length >= 7) {
                        _this.flag = false;
                        _this.getList()
                    }
                }
            }
        }
    },
    methods:{
      handleTopChange: function (status) {  //注意这个是下拉组件中的方法
        this.topStatus = status;
      },
      /*loadTop() {  // 下拉刷新数据的操作
             let token = cookie.get('token');
             this.pageNum++;
            setTimeout(()=>{
              if(this.dataList.length<7){
                this.pageNum--; //如果分页的数据小于8的话，就然他--
              }
              console.log();
               this.mycomment(token);
              this.$refs.loadmore.onTopLoaded();
            },500);
      },*/
      loadTop() {  // 下拉刷新数据的操作
            setTimeout(()=>{
              this. mycomment();
              this.$refs.loadmore.onTopLoaded();
            },500);
      },
      goArtical(id) {  //点击评论跳转到想对应得页面
            var token = cookie.get('token');
            let postData = { visitType: 2, informationId: id, token: token ,channel :window.sessionStorage.getItem('channel')?window.sessionStorage.getItem('channel'):'' };
            this.$http.post('/loansupermarket-app/information/insertInformationVisit', postData ).then(function(response) {
                if (response.body.code == '000') {
                    this.$router.push({ path: '/main/mydiscover/artical', query: { id: id } })
                } else if (response.body.code == '444') {
                    this.$router.push({ path: '/main/mydiscover/artical', query: { id: id } })
                }
            })
      },
        // 上拉加载数据
        getList() {
            this.pageNum += 1;
            this.loadMoreShow = true;
            var token = cookie.get('token');
            var _this = this;
            setTimeout(function() {
               _this.$http.post('/loansupermarket-app/personal/postListComment', { pageSize: 7, pageNum:_this.pageNum, token: token },{ headers: {token:token}})
                    .then((response) => {
                        var resBody = response.body;
                        if (resBody.code == '000') {
                           _this.flag = true;
                            _this.loadMoreShow = false;
                            if (resBody.data.length < 7) {
                                _this.noMoreShow = true;
                                _this.flag = false;
                            } else {
                                _this.flag = true;
                            }
                            _this.dataList = _this.dataList.concat(resBody.data);
                              _this.dataList.forEach(function(item, index) {
                                 item.create_time = dateFormat(item.create_time, 'YYYY-MM-DD HH:mm:ss');
                              })
                          
                        } else if(resBody.code == '444') {
                            this.$router.push({path:'/login',query:{history:1}});
                        }
                    })
            }, 500)
        },
      /*mycomment(token){  
         var _this = this;
         this.$http.post('/loansupermarket-app/personal/postListComment', { pageSize: 7, pageNum: this.pageNum, token: token },{ headers: {token:token}}).then(function(response) {
           var resBody = response.data;
           console.log(response);
           if (resBody.code == '000') {
              _this. dataList = resBody.data;
              // 如果最后一页列表的长度小于7，说明已经没有更多了
              if (_this.dataList.length==0||resBody.data.length<7) {
                 _this.noMoreShow = true; 
              }
              // 时间戳转成2018-10-08格式
              _this.dataList.forEach(function(item, index) {
                item.create_time = dateFormat(item.create_time, 'YYYY-MM-DD HH:mm:ss');
              })
            } else if (resBody.code == '444') {
              this.$router.push({ path: '/login', query: { history: 1 } });
            }else{
              MessageBox.alert(resBody.mesg);
            }
        })
      },*/
      mycomment(){  
           this.noMoreShow = false; 
           var token = cookie.get('token');
            // 加载第一页数据并实例化
            let _this=this;
            this.$http.post('/loansupermarket-app/personal/postListComment', { pageSize: 7, pageNum:1, token: token },{ headers: {token:token}})
                .then((response) => {
                    var resBody = response.body;
                    if (resBody.code == '000') {
                       this.dataList.splice(0,resBody.data.length); //下拉请求新的数据，并且将第一页的数据删掉
                      this.dataList = resBody.data.concat(this.dataList); //表示的是将最新的数据插在最前面
                     
                        if (this.dataList.length < 7) {
                            this.flag = false;
                            this.noMoreShow = true; 
                        }else {
                                this.flag = true;
                            }
                        if( this.dataList.length==0){
                             this.noMoreShow = false; 
                        }
                             // 时间戳转成2018-10-08格式
                _this.dataList.forEach(function(item, index) {
                item.create_time = dateFormat(item.create_time, 'YYYY-MM-DD HH:mm:ss');
              })
                    }else if(resBody.code == '444'){
                        this.$router.push({path:'/login',query:{history:1}});
                    }else{
                        MessageBox.alert(resBody.mesg);
                    }

                })
      },
      skit(){  //当点击去逛逛论坛时跳转到相对于的页面
          this.$router.push({path:'/main/discover/discuss'});
      },
       // 去编辑
        goEdit() {
            this.edit = !this.edit;
            if (!this.edit) {
                this.selectNum = 0;
            }
        },
         //选择复选款的功能
        singleSelect() {
            var _this = this;
            var flag = true;
            var checkboxs=document.querySelectorAll('.ss');
          this.$refs.checkbox.forEach(function(item, index) {
            //checkboxs.forEach(function(item, index) {
              if (!item.checked) {
                _this.$refs.allSelect.checked = false;
                flag = false;
                return false;
              }
              if (flag) {
                _this.$refs.allSelect.checked = true;
              }
            })
            // 判断已选列表个数
            this.judgeNum();
        },
         // 判断已选列表个数
        judgeNum() {
          var _this = this;
          _this.selectNum = 0;
          var checkboxs=document.querySelectorAll('.ss');
           this.$refs.checkbox.forEach(function(item, index) {
            //checkboxs.forEach(function(item, index) {
            if (item.checked) {
              _this.selectNum += 1
            }
          })
        },
         // 如果是全选，那么所有的checkbox全部变成已选
        allSelect() {
          var _this = this;
           var checkboxs=document.querySelectorAll('.ss');
          if (this.$refs.allSelect.checked) {
           this.$refs.checkbox.forEach(function(item, index) {
            //checkboxs.forEach(function(item, index) {
              item.checked = true;
            })
          } else {
            this.$refs.checkbox.forEach(function(item, index) {
            //checkboxs.forEach(function(item, index) {
              item.checked = false;
            })
          }
          // 判断已选列表个数
          this.judgeNum();
        },
        shanchu(){
            var _this = this;
            var token = cookie.get("token");
            // 添加了一个属性cId，根据属性获取产品id再添加到数组提交到后台
            this.$refs.checkbox.forEach(function(item, index) {
              if (item.checked) {
                _this.arr.push(item.getAttribute('cId'));
                _this.idStr = _this.arr.join(',');
              }
            })
            if (this.idStr.length == 0) {
              Toast('请勾选你要删除的帖子~');
            } else {
              this.$http.post('/loansupermarket-app/information/deleteInformationVisit', { visitType: 1, idStr: this.idStr, token: token }).then(function(response) {
                if (response.body.code == '000') {
                  Toast('删除成功！');
                  setTimeout(function() {
                    window.location.reload()
                  }, 500)
                } else if (response.body.code == '444') {
                  this.$router.push({ path: '/login', query: { history: 1 } });
                } else {
                  MessageBox.alert(response.body.mesg);
                }
              })
            }
        },    
    },
    mounted(){       
          this.mycomment();
    },
    components: { LoadMore }
}
</script>
<style lang="scss" scoped>
@import '../../../../scss/variable/base.scss';
.lo .weui-loadmore{
  margin:0px auto;
  line-height:0;
}
.lo{
  padding:1.2em 0 0 0;
}
    .mycomment{
          text-align: left;
          margin-top: 2.9rem;
          width: 100%;
          box-sizing: border-box;
          &-edit{
            position: fixed;
            right: 0.4rem;
            top: 0.35rem;
            z-index: 999;
            color: #fff;
            font-size: 0.4rem;
          }
          &-content{
            margin-bottom: rem(20);
              display: flex;
              padding: rem(30) 0.4rem;
              background: #ffffff;
             &-right{
                 p:nth-of-type(1){
                     color: #999999;
                     font-size: rem(24);
                 }
                  p:nth-of-type(2){
                     color: #333333;
                     font-size: rem(28);
                      margin: rem(18) 0 rem(30) 0;
                        // 表示的意思的如果文字超过了一行的话，就省略...
                        overflow: hidden;
                        text-overflow: ellipsis;
                        display: -webkit-box;
                        -webkit-line-clamp:2;
                        -webkit-box-orient:vertical;
                 }
                 &-content{
                     box-sizing: border-box;
                     background: #f5f5f5;
                     border-radius: 8px;
                     width:rem(650);
                     &-top{
                       padding-right: rem(20);
                        width: 100%;
                        height:rem(80);
                        border-bottom: 1px solid #e3e3e3;
                        p{
                            margin-left: rem(30);
                            font-size: rem(26);
                            color: #666666;
                            line-height: rem(80);
                        }
                     }
                    &-bottom{
                        width: 100%;
                        height:rem(70);
                        //margin-top: rem(24);
                        line-height: rem(70);
                     }
                 }
                 &-icon{ 
                   a{    
                        float: right;
                        display:block;
                        margin-right: 10px;
                        img{
                            width:rem(35);
                            height:rem(35);
                            vertical-align: middle;
                            padding-right:2px;
                            padding-bottom: 2px;
                            }
                        span{
                            font-size: rem(24);
                            color: #999999;
                            }     
                   }
                 }
             }
          }
    }
    .showimg{
         margin-top:rem(120);
           text-align: center;
           p{
              margin-top: rem(40);
               a{
                 text-decoration: underline;
                 font-size: rem(20);
                 color: #00dada;
               }
           }
    }
 // 这个是复选框的样式
.chk_1 {
    display: none; // vertical-align: -15%;
    width: 0.3rem;
    height: 0.3rem;
  }
.chk_1+label {
    color:#fff !important;
    margin-top: 1.1rem;
    background-color: #FFF;
    border: 1px solid #C1CACA;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05),
    inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
    padding: 0.2rem;
    border-radius: 50%;
    display: inline-block;
    position: relative;
    width: 0.1rem;
    height: 0.1rem; 
    margin-right: 10px;
  }
  .chk_1+label:active {

    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px 1px 3px rgba(0, 0, 0, 0.1);
  }
  .chk_1:checked+label {
    font-size: 0.17rem;
    margin-top: 1.1rem;
    background-color: $primaryColor;
    border: 1px solid $primaryColor;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 15px 10px -12px rgba(255, 255, 255, 0.1);
    color: #243441;
  }
  .chk_1:checked+label:after {
    content: '\2714'; //勾选符号 
    position: absolute;
    top: 0px;
    left: 0px;
    color: #fff;
    width: 100%;
    text-align: center;
    font-size: 1.4em;
    vertical-align: text-top;
  }
  //这个是顶部删除的样式
.deleteDo {
    display: flex;
    width: 100%;
    position: fixed;
    left: 0;
    height: 1.3rem; // line-height: 1.3rem;
    color: #fff;
    bottom: 0;
    &-left {
      flex: 6;
      background: rgba(0, 0, 0, 0.4);
      border-right: 1px solid #fff;
      text-align: left;
      box-sizing: border-box;
      padding: 0 0.4rem;
      .chk_1+label {
        margin-top: 0.35rem;
        background-color: #FFF;
        border: 1px solid #C1CACA;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
        padding: 0.2rem;
        border-radius: 50%;
        display: inline-block;
        position: relative;
        margin-right: 30px; // display: none;
        // vertical-align: -15%;
        width: 0.1rem;
        height: 0.1rem;
      }
      .chk_1:checked+label {
        margin-top: 0.35rem;
        background-color: $primaryColor;
        border: 1px solid $primaryColor;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 15px 10px -12px rgba(255, 255, 255, 0.1);
        color: #243441;
      }
      span {
        float: right;
        line-height: 1.3rem;
        font-size: 0.3rem;
      }
    }
    &-right {
        text-align: center;
      font-size: 0.3rem;
      flex: 3;
      line-height: 1.3rem;
      background: rgba(0, 0, 0, 0.4)
    }
  }
</style>
